package mo.tags.backtrack;

import java.util.ArrayList;
import java.util.List;

public class L797 {

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        int n = graph.length;
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> combination = new ArrayList<>();
        dfs(res, combination, 0, n, graph);
        return res;
    }

    private void dfs(List<List<Integer>> res, List<Integer> combination, int idx, int n, int[][] graph) {
        combination.add(idx);
        if (idx == n-1) {
            res.add(new ArrayList<>(combination));
            return;
        }
        int[] targets = graph[idx];
        for (int target : targets) {
            dfs(res, combination, target, n, graph);
            combination.remove(combination.size()-1);
        }
    }

    public static void main(String[] args) {
        L797 test = new L797();
        System.out.println(test.allPathsSourceTarget(new int[][]{{4,3,1}, {3,2,4}, {3}, {4}, {}}));
    }

}
